﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class cpassword : System.Web.UI.Page
{
    private EncryptMD5 encryptMD5 = null;
    private SQLQuery sqlQuery = null;
    private string email = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["candidate"] == null)
        {
            Response.Redirect("index.aspx");
        }
        else
        {
            email = Session["candidate"].ToString();
        }
        loadUserControls();
        encryptMD5 = new EncryptMD5();
        sqlQuery = new SQLQuery();
    }

    private void loadUserControls()
    {
        string path = "panel/menulist.ascx";
        panel_menulist.Controls.Clear();
        UserControl userControl = (UserControl)LoadControl(path);
        panel_menulist.Controls.Add(userControl);
    }

    protected void btn_changepass_Click(object sender, EventArgs e)
    {
        lbl_msg.Text = "";
        string newpass = txt_newpassword.Text.Trim();
        if (newpass.Length < 6)
        {
            lbl_msg.Text = "New " + LocalVariables.PASSWORD_CHARACTERS;
            txt_newpassword.Focus();
            return;
        }

        string oldpass = txt_password.Text.Trim();
        if (newpass.Equals(oldpass))
        {
            lbl_msg.Text = LocalVariables.NEW_OLD_PASS;
            return;
        }

        oldpass = encryptMD5.encrypt(txt_password.Text.Trim());
        if (oldpass.Equals(LocalVariables.ARRAY_INFOR_CANDIDATE[8]))
        {
            newpass = encryptMD5.encrypt(txt_newpassword.Text.Trim());
            if (sqlQuery.updateChangePassword(email.Trim(), newpass.Trim()))
            {
                lbl_msg.Text = LocalVariables.CHANGE_PASS_SUCCESS;
                LocalVariables.ARRAY_INFOR_CANDIDATE[8] = newpass;
            }
        }
        else
        {
            lbl_msg.Text = LocalVariables.OLD_PASS_INVALID;
        }
    }
}